package org.xi8ix.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Allows a generic translation of a result set row, to an object of interest.
 *
 * @author Iain Shigeoka
 */
public interface ResultMapper<T> {
    /**
     * For each row, this method is called. The mapper must map the row data
     * to an object of that type.
     *
     * @param rs the result set pointed to the current row to map
     * @param index the index of the result set result currently being pointed to
     * @return the object created
     * @throws SQLException if there is a problem accessing the data
     */
    T mapRow(ResultSet rs, int index) throws SQLException;

    public class Tools {
        /**
         * Converts a SQL date to a standard library date.
         *
         * @param date the date to convert
         * @return the date converted
         */
        public static java.util.Date fromSql(java.sql.Date date) {
            if (date == null) {
                return null;
            } else {
                return new java.util.Date(date.getTime());
            }
        }

        /**
         * Converts a SQL timestamp to a standard library date.
         *
         * @param timestamp the timestamp to convert
         * @return the date converted
         */
        public static java.util.Date fromSql(java.sql.Timestamp timestamp) {
            if (timestamp == null) {
                return null;
            } else {
                return new java.util.Date(timestamp.getTime());
            }
        }
    }
}
